package views.fin;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Usuario;
import models.fin.Categoria;

import supers.GUICadInterface;
import supers.GUIInternalFrame;
import utils.GlobalDeclarations;
import views.SisDskGUI;

@SuppressWarnings("serial")
public class FinCatGUI extends GUIInternalFrame implements GUICadInterface{
	
	public JFormattedTextField tfCod;
	public JTextField tfNome;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JCheckBox cbOrc;
	
	public FinCatGUI() {
		setTitle("Cadastro de Categorias");
		setSize(new Dimension(335, 190));
		
		try {
			tfCod = new JFormattedTextField(new MaskFormatter("###"));
			tfCod.setColumns(3);
			tfCod.setBounds(71, 25, 35, 20);
			getContentPane().add(tfCod);			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		JLabel lblCdigo = new JLabel("Cod.:");
		lblCdigo.setBounds(27, 28, 46, 14);
		getContentPane().add(lblCdigo);
		
		tfNome = new JTextField();
		tfNome.setColumns(10);
		tfNome.setBounds(71, 58, 224, 20);
		getContentPane().add(tfNome);
		
		JLabel label = new JLabel("Nome:");
		label.setBounds(27, 61, 46, 14);
		getContentPane().add(label);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel.setBounds(27, 115, 268, 26);
		getContentPane().add(panel);
		
		btNovo = new JButton("Novo");
		ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
		Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
		btNovo.setIcon(new ImageIcon(img));
		btNovo.setBounds(1, 1, 80, 23);
		panel.add(btNovo);
		
		btExcluir = new JButton("Excluir");
		btExcluir.setEnabled(false);
		ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
		Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
		btExcluir.setIcon(new ImageIcon(img2));
		btExcluir.setBounds(91, 1, 85, 23);
		panel.add(btExcluir);
		
		btListar = new JButton("Listar");
		ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
		Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
		btListar.setIcon(new ImageIcon(img3));
		btListar.setBounds(186, 1, 80, 23);
		panel.add(btListar);
		
		cbOrc = new JCheckBox("Inclusa no or\u00E7amento");
		cbOrc.setBounds(27, 85, 129, 23);
		getContentPane().add(cbOrc);
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
		tfCod.addFocusListener(fl);
		btNovo.addActionListener(ac);
		btExcluir.addActionListener(ac);
		btListar.addActionListener(ac);
	}

	@Override
	public void limpar() {
		tfCod.setText("");
		tfNome.setText("");
		cbOrc.setSelected(false);
	}

	@Override
	public void setDados(Object obj) {
		Categoria cat = (Categoria) obj;
		tfCod.setText(cat.getCodigo());
		tfNome.setText(cat.getNome());
		cbOrc.setSelected(cat.isCkOrcamento());
	}

	@Override
	public Object getDados() {
		Categoria cat = new Categoria();
		cat.setCodigo(tfCod.getText());
		cat.setNome(tfNome.getText());
		cat.setCkOrcamento(cbOrc.isSelected());
		cat.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		cat.setDtOpe(new Date(System.currentTimeMillis()));
		return cat;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}

}
